﻿

#include "zyDialogBase.h"


DarkMoonUI::zyDialogBase::zyDialogBase()
{
	SetMember(L"this", (LONG_PTR)this);
	m_obj->setWndProc(zyDialogBase_WndProc);

}
DarkMoonUI::zyDialogBase::~zyDialogBase()
{

}
LRESULT WINAPI DarkMoonUI::zyDialogBase::zyDialogBase_WndProc(zyObject* obj, HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL* bHandled)
{

	return zyForm::zyForm_WndProc(obj, hWnd, uMsg, wParam, lParam, bHandled);
}
int DarkMoonUI::zyDialogBase::取控件整数(int 参数_控件ID, BOOL& 参数_是否成功) {//GetDlgItemInt
	return GetDlgItemInt(取窗口句柄(), 参数_控件ID, &参数_是否成功, true);
}

bool DarkMoonUI::zyDialogBase::置控件整数(int 参数_控件ID, int 参数_整数值) {//SetDlgItemInt
	return SetDlgItemInt(取窗口句柄(), 参数_控件ID, 参数_整数值, 1) != 0;
}
std::wstring DarkMoonUI::zyDialogBase::取控件文本(int 参数_控件ID) {//GetDlgItemText
	std::wstring 局部_缓冲区;
	int len = 512;
	局部_缓冲区.reserve(len);
	len = GetDlgItemText(取窗口句柄(), 参数_控件ID, (LPWSTR)局部_缓冲区.c_str(), len);
	if (len == 0) {
		return L"";
	}
	if (len > 511) {
		局部_缓冲区.reserve(len * 2 + 2);
		GetDlgItemText(取窗口句柄(), 参数_控件ID, (LPWSTR)局部_缓冲区.c_str(), len * 2 + 2);
	}
	return 局部_缓冲区;
}
bool DarkMoonUI::zyDialogBase::置控件文本(int 参数_控件ID, const std::wstring& 参数_文本) {//SetDlgItemText
	return SetDlgItemText(取窗口句柄(), 参数_控件ID, 参数_文本.c_str()) != 0;
}
int DarkMoonUI::zyDialogBase::发送控件消息(int 参数_控件ID, int 参数_消息值, WPARAM 参数_wParam, LPARAM 参数_lParam) {//SendDlgItemMessage
	return (int)SendDlgItemMessage(取窗口句柄(), 参数_控件ID, 参数_消息值, 参数_wParam, 参数_lParam);
}
POINT DarkMoonUI::zyDialogBase::对话框单位到像素(int dialogUnitsX, int dialogUnitsY) {
	POINT ret = { 0 };
	HWND hwnd = reinterpret_cast<HWND>(GetMember(L"hWnd"));
	// 检查 hwnd 是否有效  
	if (hwnd == nullptr || hwnd == INVALID_HANDLE_VALUE) {
		// 如果没有有效的 hwnd，则使用默认的对话框基本单位进行转换  
		long baseunit = GetDialogBaseUnits();
		int baseunitX = LOWORD(baseunit);
		int baseunitY = HIWORD(baseunit);
		// 注意：这里的转换因子（4 和 8）可能需要根据实际情况调整  
		// 通常，这些因子取决于 DPI 和对话框单位的设计意图  
		ret.x = MulDiv(dialogUnitsX, baseunitX, 4);
		ret.y = MulDiv(dialogUnitsY, baseunitY, 8);
	}
	else {
		// 如果 hwnd 有效，则使用 MapDialogRect 进行转换  
		RECT rc = { 0, 0, static_cast<LONG>(dialogUnitsX), static_cast<LONG>(dialogUnitsY) };
		if (MapDialogRect(hwnd, &rc)) {
			// MapDialogRect 成功时，rc 将包含转换后的像素坐标  
			ret.x = rc.right - rc.left;
			ret.y = rc.bottom - rc.top;
		}
		else {
			// 如果 MapDialogRect 失败，可能需要设置错误处理或返回默认值  
			// 这里只是简单地返回了 {0, 0}  
			// 你可能希望记录错误或抛出异常  

		}
	}
	return ret;
}
POINT DarkMoonUI::zyDialogBase::像素到对话框单位(int 参数_对话框单位X, int 参数_对话框单位Y) {
	POINT ret = { 0 };
	int baseunitX, baseunitY;
	HWND hwnd = (HWND)GetMember(L"hWnd");
	if (hwnd == 0) {
		long baseunit = GetDialogBaseUnits();
		baseunitX = LOWORD(baseunit);
		baseunitY = HIWORD(baseunit);
	}
	else {
		ret.x = 4;
		ret.y = 8;
		RECT rc = {0};
		MapDialogRect(hwnd, &rc);
		baseunitX = rc.right;
		baseunitY = rc.bottom;
	}
	ret.x = MulDiv(参数_对话框单位X, 4, baseunitX);
	ret.y = MulDiv(参数_对话框单位Y, 8, baseunitY);
	return (ret);
}
void DarkMoonUI::zyDialogBase::_MoveToStartPosition(HWND hWnd) {
	RECT rc = { 0 };
	POINT size = { 0 };
	POINT pos = { 0 };
	GetWindowRect(hWnd, &rc);
	pos.x = rc.left;
	pos.y = rc.top;
	size.x = rc.right - rc.left;
	size.y = rc.bottom - rc.top;
	_开始位置到坐标(GetParent(hWnd), (int)GetMember(L"StartPosition"), size, pos, true);
	MoveWindow(hWnd, pos.x, pos.y, size.x, size.y, true);
}